package com.app.mvc.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.app.mvc.dao.AbstractHibernateDaoSupport;
import com.app.mvc.dao.DealDao;
import com.app.mvc.model.Deal;
import com.app.mvc.ultil.HibernateUtil;

public class DealDaoImpl extends AbstractHibernateDaoSupport<Deal, Long>
		implements DealDao {

	public DealDaoImpl() {
		super(Deal.class);
		// TODO Auto-generated constructor stub
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Deal> getDealByCategory(long ID) {
		// TODO Auto-generated method stub
		Criteria criteria = HibernateUtil.getSession()
				.createCriteria(Deal.class)
				.setFetchMode("deal", FetchMode.JOIN)
				.add(Restrictions.eq("category.id", ID)).setMaxResults(8);
		return criteria.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Deal> newDeal() {
		// TODO Auto-generated method stub
		Criteria criteria = HibernateUtil.getSession()
				.createCriteria(Deal.class)
				.setFetchMode("deal", FetchMode.JOIN).setMaxResults(8)
				.addOrder(Order.desc("id"));
		return criteria.list();
	}

	@Override
	public int numsDeal() {
		// TODO Auto-generated method stub
		Criteria criteria = HibernateUtil.getSession()
				.createCriteria(Deal.class)
				.add(Restrictions.eq("position", "order"))
				.setProjection(Projections.rowCount());
		return ((Integer) criteria.list().get(0)).intValue();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Deal> getDealByPage(int page, int number) {
		// TODO Auto-generated method stub
		Criteria criteria = HibernateUtil.getSession().createCriteria(
				Deal.class);
		criteria.add(Restrictions.eq("position", "order"));
		criteria.setFirstResult(page);
		criteria.setMaxResults(number);
		criteria.addOrder(Order.desc("id"));
		return criteria.list();
	}

}
